<?php
header('Content-type:text/html;charset=utf8');
/**
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-4-11
*/

class OrderController extends Controller{
	public $cart_total_count = 0;
	/**
	 * 通过用户访问控制器过滤实现页面缓存
	 */
//	function filters(){
//		return array(
//			array(
//				'system.web.widgets.COutputCache + details',
//				'duration'=>1800,
//				'varyByParam'=>array('_goods_id'),//id变化 
//				
//			),//类过滤器
//		);	
//	}
	
	/**
	 * 订单展示
	 */
	
	function filters(){
		return array(
			'accessControl'
		);
	}
/**
	 * 具体方法访问具体条件
	 */
	
	function accessRules(){
		return array(
			array(
				'allow',
				'actions'=>array('Order','cart','Cartadd','cartdel','cartupd','account','orderconfirm','pay','payfor','payall','payforall','ordercancel'),
				'users'=>array('@'),//登陆系统的用户
			),
			array(
				'deny',
				'users'=>array('*'),//所有用户
			),
		);
	}
	function actionOrder($user_id){
		if($user_id == Yii::app()->user->id){
			$cart_model = new Cart();
			$cart_total_count = $cart_model->getCartTotalCountByUser_id(Yii::app()->user->id);
		 	$cart_total_count = $cart_total_count['0']['count_total'];
			Yii::app()->params['cart_total_count'] = $cart_total_count;
			$this->layout='//layouts/order';
			$order_model = new Order();
			$cccmodel = new CCCModel();
			
	//		$order_info = $order_model->getOrderByUserId($user_id);
			$order_nums = $order_model->getOrderNumsByUserId($user_id);
			$goods_array = array();
		
		/**
		 * 未实现功能
		 *
		*for ($i = 0; $i < count($order_nums); $i++) {
		*	if($order_nums[$i]['order_nums'] > 1){
		*		$info = $order_model->getGoodsByOrder_no($order_nums[$i]['order_no']);
		*			for ($j = 0; $j < count($info); $j++) {
		*				array_push($goods_array,$info[$j]['goods_id']);
		*			}
		*			$goods_array = implode($goods_array,',');
		*			$_sql = "SELECT goods_name,goods_price,goods_thumbnail_url FROM {{goods}} WHERE goods_id IN($goods_array)";
		*			$goods_info = $cccmodel->sel($_sql);
		*			array_push($goods_info,$order_nums[$i]['order_no']);
		*	}else{
		*			$info = $order_model->getGoodsByOrder_no($order_nums[$i]['order_no']);
		*			$_sql = "SELECT goods_name,goods_price,goods_thumbnail_url FROM {{goods}} WHERE goods_id =".$info[0]['goods_id'];
		*			$goods_info = $cccmodel->sel($_sql);
		*			array_push($goods_info,$order_nums[$i]['order_no']);
		*	}
		*}*/
		

			$this->render('order',array('order_nums'=>$order_nums));
		}else{
			echo "非法操作,请返回继续操作";
			exit;
		}
	} 
	
	function actionCart(){
		
		if(!empty(Yii::app()->user->id)){
			$this->layout='//layouts/order';
			$cart_model = new Cart();
			$cart_info = $cart_model->selCartByuserId(Yii::app()->user->id);
		 	$cart_total_count = $cart_model->getCartTotalCountByUser_id(Yii::app()->user->id);
		 	$cart_total_count = $cart_total_count['0']['count_total'];
			Yii::app()->params['cart_total_count'] = $cart_total_count;
			$this->render('cart',array('cart_info'=>$cart_info));
		}else{
			echo "非法操作,请返回继续操作";
			exit;
		}
	}
	
	function actionCartadd($goods_id){
		$cart_model = new Cart();
		$res = $cart_model->selUserGoods(Yii::app()->user->id,$goods_id);
		if(count($res)){
			$row = $cart_model -> updProduct(Yii::app()->user->id,$goods_id);
		}else{
			$row = $cart_model -> addProduct(Yii::app()->user->id,$goods_id,1,time());
		}
		if($_POST['type'] == "addpro"){
			if($row){
				echo 1;
				exit;
			}else{
				echo 0;
				exit;
			}
		}
	}
	
	/**
	 * 移除购物车信息
	 * @param $goods_id
	 */
	function actioncartdel($goods_id){
		$cart_model = new Cart();
		$row = $cart_model -> delcart($goods_id,Yii::app()->user->id);
		if($_POST['type'] == 'delpro'){
			if($row){
				echo 1;
				exit;
			}else{
				echo 0;
				exit;
			}
		}
	}
	
	/**
	 * 更新购物车信息
	 */
	function actioncartupd(){
		$cart_model = new Cart();
		
		if($_POST['send'] == '更新购物车'){
				for ($i = 0; $i < count($_POST['goods_ids']); $i++) {
					$cart_model->updcart(Yii::app()->user->id,$_POST['goods_ids'][$i],$_POST['cart_goods_count'][$i]);
				}
					$cart_model->updcartactive(Yii::app()->user->id,$_POST['active']);
					$this->redirect('./index.php?r=order/cart');
			}
	}
	/**
	 * 结算-确认订单
	 */
	function actionaccount(){
		$cart_model = new Cart();
		$addr_model = new Addr();

		$addr_info = $addr_model -> getAddrByUserId(Yii::app()->user->id);

		$goods_info = $cart_model->GetActiveGoods(Yii::app()->user->id);
		$this->render('orderconfirm',array('goods_info'=>$goods_info,'addr_info'=>$addr_info));
	}
	/**
	 * 结算-创建订单
	 */
	function actionorderconfirm(){
		
		$user = new User();
		$email = $user -> getEmailByUserId(Yii::app()->user->id);
		$email = $email['0']['user_email'];
		$order = new Order();

		$uniqid = $_POST['unid'];
		$active =  $this->checkRepeatSubmit($uniqid,300);//判断是否重复提交的时间,1为不重复
		
		//生成订单号
		$order_no = rand(1,1000).time();
		if($_POST['pay'] == '创建订单' && $active){
			for ($i = 0; $i < count($_POST['goods_ids']); $i++) {
				$row = $order->createOrderDetails($order_no,$_POST['goods_ids'][$i],$_POST['cart_goods_count'][$i],$_POST['price_per'][$i]);
			}
				$row2 = $order->createOrder($order_no,$_POST,Yii::app()->user->id);
			if($row && $row2){
				$email = new email($email,$order_no);
				$email->email_send(Yii::app()->user->id);
				$order->delAllCart(Yii::app()->user->id);
				$this->redirect("./index.php?r=order/payall&order_no=$order_no&price=$_POST[price_total]");
			}
		}else{
			echo "重复提交,请重新操作";
		}
	}
	
	/**
	 * 支付页面
	 * @param $order_no
	 */
	
	function actionpay($order_no,$goods_id=0,$price){
		$order_model = new Order();
		$express_fee = $order_model->getExpressFeeByOrder_no($order_no);
		$express_fee = $express_fee['0']['express_fee'];
		$row = $order_model -> checkPay($order_no,$goods_id,$price-$express_fee);
		if(!empty($row)){
			$this->render('pay',array('order_no'=> $order_no,'price'=>$price,'goods_id'=>$goods_id));
		}else{
			echo "非法操作,请返回继续操作";
			exit;
		}
	}
	
	/**
	 * 支付不带goods_id的订单
	 * @param $order_no
	 * @param $goods_id
	 * @param $price
	 */
	function actionpayall($order_no,$price){
			$order_model = new Order();
			$this->render('pay',array('order_no'=> $order_no,'price'=>$price,'all'=>1));
	}
	
	/**
	 * 付款不带goods_id的订单
	 * @param $order_no
	 * @param $goods_id
	 * @param $price
	 */
		function actionpayforall(){
			$order_model = new Order();
			$info = $order_model->changeGoodsStatus($_POST['order_no']);
			if($info){
				echo "支付成功";
			}else{
				echo "支付失败";
			}
	}
	
	/**
	 * 支付功能实现
	 */
	function actionpayfor(){
		$order_model = new Order();
		if($_POST['send'] == '支付'){
			$row = $order_model->changeOrderStatus($_POST['order_no'],$_POST['goods_id']);
			$status = $order_model -> getGoodsByOrder_no($_POST['order_no'],$_POST['goods_id']);
			$status = $status[0]['order_status_id'];
	
			if($row && $status != 6){
				echo "支付成功";
			}else{
				echo "支付失败";
			}
		}
	}

	/**
	 *用户取消订单
	 */
	public function actionordercancel($order_no,$goods_id){


		$order_model = new Order();
		$row = $order_model -> orderCancel($order_no,$goods_id);
		$this->redirect("./index.php?r=order/order&user_id=".Yii::app()->user->id);
		

	}
	
	/**
	 * 判断订单是否已经过期
	 * @param String $uniqid
	 * @param int $expire
	 * @return 1 过期(没有重复提交),2不过期(重复提交)
	 */
	function checkRepeatSubmit($uniqid = '', $expire_time = 300){
		$token = sha1(md5(md5($uniqid)));
		$time = time();
		if(isset($_SESSION['token']) && !empty($_SESSION['token']) && $_SESSION['token'] == $token && ($time-$_SESSION['expire_time'] < $expire_time)){
			return false;
		}else{
			$_SESSION['token'] = $token;
			$_SESSION['expire_time'] = $time;
			session_write_close();
			$this->cancelRepeatSubmit();
			return true;
		}
	}
	
	/**
	 * 清除session
	 */
	function cancelRepeatSubmit() {
        unset($_SESSION['token']);
        unset($_SESSION['expire_time']);
    }
}
?>